package br.com.simora.crawler;

import java.util.Date;

import javax.inject.Inject;

import org.apache.log4j.Logger;

import br.com.simora.execucao.Execucao;
import br.com.simora.execucao.ExecucaoService;
import br.com.simora.execucao.LogCaptura;
import br.com.simora.execucao.LogErroCapturaDAO;
import br.com.simora.http.Pagina;
import br.com.simora.http.RecuperadorInformacoes;
import br.com.simora.musica.Artista;
import br.com.simora.musica.Musica;
import br.com.simora.radio.Radio;

public abstract class AbstractCrawler implements SimoraCrawler {
	org.apache.log4j.Logger logger = Logger.getLogger(AbstractCrawler.class);
	private Execucao anterior;
	protected Radio radio;
	@Inject private ExecucaoService service;
	@Inject public RecuperadorInformacoes recuperador;
	@Inject private LogErroCapturaDAO logErroCapturaDAO;
	public Execucao execucao = null;
	public Musica musica = null;
	public Artista artista = null;
	public Pagina pagina = null;
	public String radioNome = "";
	protected LogCaptura log;

	@Override
	public void run() {
		Thread thread = Thread.currentThread();
		setRadioNome();
		thread.setName("SIMORA - " + radioNome);
		while(true){
			try {
				log = new LogCaptura();
				log.setDataHoraInicio(new Date());
				log.setRadio(radio.getId());
				recuperaInfo();
				montaExecucao();
				if(execucao != null){
					gravaExecucao(execucao);
				} else {
					if("".equals(log.getRetornoFP()) || null == log.getRetornoFP()){
						log.setRetornoFP("");
					}
					log.setDataHoraFim(new Date());
					log.setStatus("E");
					log.setRadio(radio.getId());
					if(radio.getId() != null){
						logErroCapturaDAO.update(log);
					}
				}
			} catch (Exception e1) {
				logErroCapturaDAO.gravaErro(radio, e1.getMessage(), new Date());
				e1.printStackTrace();
			}
			try {
				Thread.sleep(120000);
			} catch (InterruptedException e) {
				logErroCapturaDAO.gravaErro(radio, e.getMessage(), new Date());
				logger.info(e.getMessage());
			}
			log = null;
		}
	}
	
	private void montaExecucao(){
		if(musica != null && artista != null
			&& musica.getNome() != "" && artista.getNome() != ""){
			Radio radio = new Radio();
			execucao = new Execucao();
			radio.setNome(getRadioNome());
			musica.setArtista(artista);
			execucao.setDataHora(new Date());
			execucao.setMusica(musica);
			execucao.setRadio(radio);
		}
	}

	private String getRadioNome() {
		return this.radioNome;
	}

	@Override
	public void gravaExecucao(Execucao execucao) {
		if(anterior == null){
			anterior = execucao;
			service.processoInserirExecucao(execucao, log);
		} else {
			if(!execucao.getMusica().getNome().equals(anterior.getMusica().getNome())
				&& !execucao.getMusica().getArtista().getNome().equals(anterior.getMusica().getArtista().getNome())){
				service.processoInserirExecucao(execucao, log);
				anterior = execucao;
			} else {
				log.setStatus("S");
				log.setDataHoraFim(new Date());
				log.setRadio(radio.getId());
				if(radio != null && radio.getId() != null){
					if(null == log.getRetornoFP()){
						log.setRetornoFP("ALGUM ERRO GRAVE");
					}
					logErroCapturaDAO.save(log);
				}
			}
		}
	}

}